package com.fuqi.android.zhongcai.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.view.activity.LoginActivity;

import static com.google.gson.internal.$Gson$Preconditions.checkNotNull;

public class ActivityUtils {

    public static void startActivity(Context context, Class<?> clz) {
        Intent intent;
        intent = new Intent(context, clz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void startActivityNeedLogin(Context context, Class<?> clz) {

        startActivityNeedLogin(context,clz,null);
    }

    public static void startActivityNeedLogin(Context context, Class<?> clz, Bundle bundle) {

        Intent intent;
        if (UniApplication.getInstance().hasLogin()){

            intent = new Intent(context, clz);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (bundle != null) {
                intent.putExtras(bundle);
            }
            context.startActivity(intent);

        } else {

            intent = new Intent(context, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            System.exit(0);
        }


    }

    public static void startActivity(Context context, Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(context, clz);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


    public static void toast(String content) {

        Toast.makeText(UniApplication.getInstance(), content, Toast.LENGTH_SHORT).show();
    }

    public static void closeInputKeyboard(Context context) {
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(),

                InputMethodManager.HIDE_NOT_ALWAYS);
    }


    public static void mailTo(Context context, String mailAddress,
                              String subject, String text) {
        Intent data = new Intent(Intent.ACTION_SENDTO);
        data.setData(Uri.parse("mailto:" + mailAddress));
        data.putExtra(Intent.EXTRA_SUBJECT, subject);
        data.putExtra(Intent.EXTRA_TEXT, text);
        context.startActivity(data);
    }

    public static void callTo(Context context, String phoneNumber) {
        Intent phoneIntent = new Intent(Intent.ACTION_DIAL,

                Uri.parse("tel:" + phoneNumber));
        phoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(phoneIntent);
    }

    public static void toIntnet(Context context, String url) {
        Uri uri = Uri.parse(url);
        Intent urlIntent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(urlIntent);
    }

    public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
                                              @NonNull Fragment fragment, int frameId) {
        checkNotNull(fragmentManager);
        checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }
}
